package com.dlc.commonlibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.dlc.commonlibrary.R;

/**
 * Created by John on 2017/7/18.
 */

public class CommonItemView extends FrameLayout {

    ImageView mLabelImage;
    TextView mLabel;
    TextView mContent;
    ImageView mArrow;
    View mBottomLine;

    public CommonItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        setClickable(true);

        View view = LayoutInflater.from(context).inflate(R.layout.view_common_item, this, true);

        mLabelImage = (ImageView) view.findViewById(R.id.label_image);
        mLabel = (TextView) view.findViewById(R.id.label);
        mContent = (TextView) view.findViewById(R.id.content);
        mArrow = (ImageView) view.findViewById(R.id.arrow);
        mBottomLine = view.findViewById(R.id.bottom_line);

        int defaultContentColor = ContextCompat.getColor(context, R.color.item_content_color);
        int defaultLableColor = ContextCompat.getColor(context, R.color.item_label_color);
        int defaultLineColor = ContextCompat.getColor(context, R.color.item_bottom_line_color);
        float defaultTextSize = getResources().getDimension(R.dimen.normal_26dp);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonItemView);

        // 颜色
        int color;

        color = ta.getColor(R.styleable.CommonItemView_label_color, defaultLableColor);
        mLabel.setTextColor(color);
        color = ta.getColor(R.styleable.CommonItemView_content_color, defaultContentColor);
        mContent.setTextColor(color);
        color = ta.getColor(R.styleable.CommonItemView_bottom_line_color, defaultLineColor);
        mBottomLine.setBackgroundColor(color);

        // 标签图片
        int labelImageRes = ta.getResourceId(R.styleable.CommonItemView_label_image, 0);
        if (labelImageRes != 0) {
            mLabelImage.setImageResource(labelImageRes);
        }

        // 大小
        float dimen;
        dimen = ta.getDimension(R.styleable.CommonItemView_label_size, defaultTextSize);
        mLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, dimen);
        dimen = ta.getDimension(R.styleable.CommonItemView_content_size, defaultTextSize);
        mContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, dimen);

        // 文本
        String text;
        text = ta.getString(R.styleable.CommonItemView_label);
        mLabel.setText(text);
        text = ta.getString(R.styleable.CommonItemView_content);
        mContent.setText(text);

        // 是否显示
        boolean show;
        show = ta.getBoolean(R.styleable.CommonItemView_show_label_image, labelImageRes != 0);
        showIt(mLabelImage, show);
        show = ta.getBoolean(R.styleable.CommonItemView_show_bottom_line, true);
        showIt(mBottomLine, show);
        show = ta.getBoolean(R.styleable.CommonItemView_show_content, true);
        showIt(mContent, show);
        show = ta.getBoolean(R.styleable.CommonItemView_show_right_arrow, false);
        showIt(mArrow, show);

        ta.recycle();
    }

    private void showIt(View view, boolean show) {
        view.setVisibility(show ? VISIBLE : GONE);
    }

    public void setLabel(CharSequence label) {
        mLabel.setText(label);
    }

    public void setLabel(int resId) {
        mLabel.setText(resId);
    }

    public void setContent(CharSequence content) {
        mContent.setText(content);
    }

    public void setContent(int resId) {
        mLabel.setText(resId);
    }

    public TextView getLabel() {
        return mLabel;
    }

    public String getLabelText() {
        return mLabel.getText().toString();
    }

    public TextView getContent() {
        return mContent;
    }
    
    public String getContentText() {
        return mContent.getText().toString();
    }

    public void setLabelImage(int resId) {
        mLabelImage.setImageResource(resId);
    }
}
